﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.Services/PathDirProvider
* 类       名: PathDirProvider
* 功      能:  TD
* 时      间:  2024-05-06 9:57:46
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using SiliconSelect.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WaferSelectV3.Common;
namespace WaferSelectV3.Services
{
    public class PathDirProvider
    {
        public static void CreateDir()
        {
            string defaultFilePath = @"Assets\Files\DefaultRecipe.xlsx";
            if (!Directory.Exists(RecipeImportDir))
            {
                Directory.CreateDirectory(RecipeImportDir);
                // 复制文件
                File.Copy(defaultFilePath, Path.Combine(RecipeImportDir, RecipeImportFilename), overwrite: true);
            }

            if (!Directory.Exists(ProductionRecipeDir))
            {
                Directory.CreateDirectory(ProductionRecipeDir);
                // 复制默认的配方文件
                File.Copy(defaultFilePath, Path.Combine(ProductionRecipeDir, ProductionRecipeName), overwrite: true);
            }
        }

        /// <summary>
        /// 创建汇总目录
        /// </summary>
        public static void CreateSummaryDir(string crystalName)
        {
            // 1. 首先是检查时间和对应的日志文件是否存在.如果不存在就创建新目录,如果已经存在了就不要创建新目录了.8点以前按照昨天的日志目录进行
            if (!Directory.Exists(SummaryByDayDir))
            {
                Directory.CreateDirectory(SummaryByDayDir);
            }

            TimeSpan nowDt = DateTime.Now.TimeOfDay;
            if (nowDt < DateTime.Parse("8:00").TimeOfDay)
            {
                SummaryByDayPath = Path.Combine(SummaryByDayDir, $@"ZY01_{DateTime.Now.AddDays(-1).ToString("yyyyMMdd")}_summary.csv");
            }
            else
            {
                SummaryByDayPath = Path.Combine(SummaryByDayDir, $@"ZY01_{DateTime.Now.ToString("yyyyMMdd")}_summary.csv");
            }
            if (!File.Exists(SummaryByDayPath))
            {
                FileOperateHelper.CreateAnEmptyFile(SummaryByDayPath);
            }

            // 2. 将晶棒编号汇总的目录和路径创建
            string summaryByCrystalNumberDir = Path.Combine(SummaryByCrystalNumberDir, $@"{DateTimeHelper.GetDateTimeForSummaryDir()}");
            if (!Directory.Exists(summaryByCrystalNumberDir))
            {
                Directory.CreateDirectory(summaryByCrystalNumberDir);
            }

            SummaryByCrystalNumberPath = Path.Combine(summaryByCrystalNumberDir, $@"ZY01_{crystalName}.csv");
            if (!File.Exists(SummaryByCrystalNumberPath))
            {
                FileOperateHelper.CreateAnEmptyFile(SummaryByCrystalNumberPath);
                CsvHelper.WriteToCsvSingleLine(SummaryByCrystalNumberPath, ConstantProvider.SummaryTitle);
            }

            // 3. 汇总导出的默认目录,按照月份去保存
            string summaryExportDir = Path.Combine(SummaryExportDir, @$"{DateTime.Now.Month}月");
            if (!Directory.Exists(summaryExportDir))
            {
                Directory.CreateDirectory(summaryExportDir);
            }
            // 汇总导出的完整路径.
            SummaryExportPath = summaryExportDir;
        }


        // 导入配方的默认文件夹,在配方设置界面进行设置时使用
        public static string RecipeImportDir = @"C:\配方文件夹";
        // 导入的配方的默认文件名
        public static string RecipeImportFilename = @"DefaultRecipe.xlsx";
        // 生产配方的目录
        public static string ProductionRecipeDir = @"C:\生产配方";
        // 生产配方的文件名
        public static string ProductionRecipeName = "DefaultRecipe.xlsx";
        // 加载到的检测规则的Rule,主要是用来测试和验证规则加载是否正确
        public static string DetectedRulePath = Path.Combine(ProductionRecipeDir, @"DetectedRuleUsed.txt");


        // 汇总目录
        public static string SummaryByDayDir = @"C:\结果汇总\ZY01";
        public static string SummaryByCrystalNumberDir = @"C:\结果汇总\ZY01";

        // 汇总文件路径带文件名的
        public static string SummaryByDayPath = "";
        public static string SummaryByCrystalNumberPath = "";

        // 汇总导出默认目录
        public static string SummaryExportDir = @"C:\结果汇总\导出汇总";
        public static string SummaryExportPath = "";

    }
}
